package com.software.sqlite.api.app.v1.controller;
import com.software.sqlite.api.admin.v1.controller.BaseController;
import com.software.sqlite.api.admin.v1.vo.AdminLoginUserVO;
import com.software.sqlite.api.app.v1.service.impl.AppV1Service;
import com.software.sqlite.api.app.v1.vo.AppV1Result;
import com.software.sqlite.common.CommonContant;
import com.software.sqlite.utils.CommonUtils;
import com.software.sqlite.utils.HutoolCacheUtils;
import com.software.sqlite.utils.VerifyCodeUtils;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;


@Api(value = "APP端-v1接口", tags = "APP端-v1接口")
@RestController
@Slf4j
@RequestMapping("/app/v1")
@CrossOrigin
public class AppV1Controller extends BaseController {

    @Resource
    private AppV1Service appV1Service;
    @Resource
    @Value("${weixin.app.appId}")
    private String appId;
    @Value("${weixin.app.appSecret}")
    private String appSecret;


    @GetMapping("/verifyCode")
    public void verifyCode(String type,String uuid,HttpServletRequest request, HttpServletResponse response) {
        try {
            if(CommonUtils.isBlank(type)){
                type="login";
            }
            OutputStream outputStream=response.getOutputStream();
            int w = 200, h = 80;
            String code = VerifyCodeUtils.generateVerifyCode(4);
            HttpSession session = request.getSession();
            session.setAttribute(CommonContant.APP_VERIFY_CODE_SESSION, code);
            if(CommonUtils.isNotBlank(uuid)){
                String key=type+"_verifyCode_"+uuid;
                HutoolCacheUtils.put(key,code);
                String getCode=CommonUtils.parseStringData(HutoolCacheUtils.get(key),"verifyCodeError");
                log.info(key+"："+getCode);
            }
            VerifyCodeUtils.outputImage(w, h, outputStream, code);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @ApiOperation(value = "登录", notes = "登录")
    @GetMapping("/login")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "账号", required = true, defaultValue = "admin"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, defaultValue = "123321"),
            @ApiImplicitParam(name = "verifyCode", value = "验证码", required = true, defaultValue = "123321")
    })
    public AppV1Result<AdminLoginUserVO> login(@ApiParam(value = "账号") String userName,
                                               @ApiParam(value = "密码") String password,
                                               @ApiParam(value = "验证码") String verifyCode,
                                               HttpServletRequest request){
        AdminLoginUserVO loginUser=appV1Service.login(userName,password,verifyCode,request);
        AppV1Result result=new AppV1Result();
        result.setSuccess(loginUser==null?false:true);
        result.setErrMsg(loginUser.getMsg());
        result.setData(loginUser);
        return result;
    }

    @ApiOperation(value = "token", notes = "token")
    @GetMapping("/token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, defaultValue = "token")
    })
    public AppV1Result token(@ApiParam(value = "token") String token){
        return AppV1Result.success(appV1Service.getTokenTime(token),0,0);
    }





}
